package me.trofoto.romeo.dao;

import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import me.trofoto.romeo.bean.User;

@Repository
public class UserDao extends BaseDao {

	public static final String TABLE_NAME = "tbl_user";

	private static final RowMapper<User> ROW_MAPPER = (rs, rowNumber) -> {
		return new User(rs.getInt("id"), rs.getString("ut_nickname"), rs.getString("ut_portrait"));
	};

	public int insert(User user) {
		return getNamedParameterJdbcTemplate().update(
				"insert into " + TABLE_NAME + " (ut_nickname, ut_portrait, ut_password) values (:nickname, :portrait, 'xxxxxxxxxxx')",
				new BeanPropertySqlParameterSource(user));
	}

	public User getById(int id) {
		return getNamedParameterJdbcTemplate().queryForObject("select * from " + TABLE_NAME,
				new MapSqlParameterSource(":id", id), ROW_MAPPER);
	}

	public List<User> getAllUsers() {
		return getNamedParameterJdbcTemplate().query("select * from " + TABLE_NAME, ROW_MAPPER);
	}

}
